import undetected_chromedriver as uc
from lxml import etree
from selenium.webdriver.common.by import By
from dbse.mysql.db_mysql_insert import addThsYJYG
from dbse.mysql.db_entity_stock_ths_yjyg import StockThsYJYG
from selenium.webdriver import ChromeOptions
from date.dateUtil import isToday
import time
import pymysql
from file.readFile import read
import datetime

'''
同花顺业绩预告
'''

def sel_report_page(browser,db,sql,page):
    endTag = False
    content = browser.page_source
    html = etree.fromstring(content,etree.HTMLParser())
    result = html.xpath('//tbody//tr')      # tbody 下的每一行数据
    for index, tr in enumerate(result):
        tds = tr.getchildren()
        print(f"====index:{index}")
        print(tds[1].find('a').text)    # 股票代码
        print(tds[2].find('a').text)    # 股票名称
        print(tds[3].find('span').text) #业绩预告类型
        print(tds[4].find('a').text)    #业绩预告摘要
        print(tds[5].text.strip())      # 净利润变动幅度
        print(tds[6].text)              # 上年同期净利润
        print(tds[7].text)              #公告日期

        entity = StockThsYJYG(tds[1].find('a').text,
                                      tds[2].find('a').text,
                                      tds[3].find('span').text,
                                      tds[4].find('a').text,
                                      tds[5].text.strip(),
                                      tds[6].text,
                                      tds[7].text,
                              tds[7].text)
        print(entity)
        addThsYJYG(db,sql,entity)

driver_path = r'C:\Program Files\Google\Chrome\Application\chromedriver.exe'
option = ChromeOptions()
option.add_argument('--headless')
browser = uc.Chrome(driver_executable_path=driver_path)
browser.set_window_size(1366,768)
browser.get('https://data.10jqka.com.cn/financial/yjyg/')
print('====begin====')

selContinue = True      # 是否继续抓取
page = 1                # 当前抓取的页码
db = pymysql.connect(host='39.98.82.109',user='root',password='landleaf@123.com',port=3306,db='stock')
sql = 'INSERT INTO stock_ths_yjyg (stock_code, stock_name,yjyg_type,yjyg_desc,lrbdfd,sntqjlr,inform_date,yjyg_period) VALUES (%s, %s,%s,%s,%s, %s,%s,%s)'

print(browser.page_source)
# sel_report_page(browser,db,sql,page)
while selContinue:
    sel_report_page(browser,db,sql,page)
    page = page +1
    # 触发下一页
    nextPage = browser.find_element(By.XPATH,f"//a[@page='{page}']")
    nextPage.click()
    time.sleep(2)  #等待页面加载
    # 跳出条件
    if page > 30 :
        selContinue = False

